package com.doubi.wxpay;

import com.ijpay.core.enums.SignType;
import com.ijpay.core.enums.TradeType;
import com.ijpay.core.kit.WxPayKit;
import com.ijpay.wxpay.WxPayApi;
import com.ijpay.wxpay.model.UnifiedOrderModel;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class WxUtil {


    public Map<String, String> suspendPayment(String ip, String orderOid, BigDecimal price) {
        price = new BigDecimal("0.01");
        price = price.multiply(new BigDecimal(100));
        // 构建请求体
        Map<String, String> params = UnifiedOrderModel
                .builder()
                .appid(WxConfig.appId)
                .mch_id(WxConfig.mchId)
                .nonce_str(WxPayKit.generateStr())
                .body("完美宴会订单")
                .out_trade_no(orderOid)
                .total_fee(String.valueOf(price.intValue())) // 单位是分
                .spbill_create_ip(ip) // 终端ip
                .notify_url(WxConfig.notifyUrl)
                .trade_type(TradeType.APP.getTradeType())
                .build()
                .createSign(WxConfig.mchKey, SignType.MD5);
        // 发送请求
        String xmlResult = WxPayApi.pushOrder(false, params);
        Map<String, String> resultMap = WxPayKit.xmlToMap(xmlResult);
        // 二次签名
        Map<String, String> packageParams = new HashMap<>();
        packageParams.put("appid", WxConfig.appId);
        packageParams.put("partnerid", WxConfig.mchId);
        packageParams.put("prepayid", resultMap.get("prepay_id"));
        packageParams.put("package", "Sign=WXPay");
        packageParams.put("noncestr", resultMap.get("nonce_str"));
        packageParams.put("timestamp", System.currentTimeMillis() / 1000 + "");
        String packageSign = WxPayKit.createSign(packageParams, WxConfig.mchKey, SignType.MD5);
        packageParams.put("sign", packageSign);
        packageParams.put("return_code", "SUCCESS");
        return packageParams;
    }
}
